package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.GoodsCategory;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.Categories2ListAdapter;

public class CategoryLevel2Layout extends LinearLayout implements
		OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private RelativeLayout loadingLayout ;
	
	private GoodsCategory parent ;
	private Categories2ListAdapter listAdapter;
	private Categories2ListAsync categoriesAsync;

	// 构造函数
	public CategoryLevel2Layout(Context context, final ViewFlipper viewFlipper,
			GoodsCategory goodsCategory) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		this.parent = goodsCategory ;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.common, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("Category");
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.GONE);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText(goodsCategory.getCatName());
		loadingLayout = (RelativeLayout)baseView.findViewById(R.id.loadingLayout);
		
		bodyList.setSelector(R.drawable.product_list_bg);
		// item 单击事件
		bodyList.setOnItemClickListener(moreClieckListener);
		listAdapter = new Categories2ListAdapter(context, bodyList);
		bodyList.setAdapter(listAdapter);

		categoriesAsync = new Categories2ListAsync(context);
		categoriesAsync.execute((Void) null);
	}

	// ListView 点击事件
	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			GoodsListLayout goodsListLayout = new GoodsListLayout(
					context, viewFlipper,parent.getCatName(), listAdapter.getItem(arg2));
			viewFlipper.addView(goodsListLayout);
			viewFlipper.showNext();
		}
	};

	class Categories2ListAsync extends
			AsyncTask<Void, Integer, ArrayList<GoodsCategory>> {
		ProgressDialog pdialog;
		long goodsCount = 1;
		boolean desc;

		public Categories2ListAsync(Context context) {
//			pdialog = new ProgressDialog(context);
//			pdialog.setTitle("Loading...");
//			pdialog.setMessage("Please wait...");
//			pdialog.setCancelable(true);
//			pdialog.setButton("cancel", new DialogInterface.OnClickListener() {
//				public void onClick(DialogInterface dialog, int i) {
//					dialog.cancel();
//					Categories2ListAsync.this.cancel(true);
//				}
//			});
//			pdialog.show();
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}
		
		@Override
		protected ArrayList<GoodsCategory> doInBackground(Void... params) {
			return WebServiceClient.getCategoryLevel2(parent.getCatId());
		}

		@Override
		protected void onPostExecute(ArrayList<GoodsCategory> result) {
			listAdapter.clear();
			listAdapter.addItems(result);
			listAdapter.notifyDataSetChanged();
//			pdialog.dismiss();
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:
			break;
		default:
			break;
		}

	}
}
